<?php 
class AdType extends Object
{
	public $AdsCount;
	public $FormId;
	public $FormBuyId;
	public $ProductId;
	public $ProductBuyId;
	public $ProductRentId;
	public $AdTypeObject;
	public $AdCats;
	public $Title;
	public $TitleEn;
	public $Description;
	public $Image;
	public $ImageObject;
	public $Active;
	
	public function AdType($values = '')
	{ 
		self::setDataTable('am_types');
		if(is_array($values))
		{ self::setValues($values); }
	}
	
	public function getTitle($forDb=0)
	{
		global $adc;
		global $txt;
		if(($forDb == 0) && ($adc->getFrontend() == 1))
		{
			if($txt->getLanguage() == 'en')
			{ return $this->TitleEn; }
			else
			{ return $this->Title; }
		}
		else
		{ return $this->Title; }
	}
	
	public function getLink()
	{ return '/adtype/'.self::getId().'/'; }
	
	public function getAdsCount() 
	{ return intval($this->AdsCount); }
	
	public function loadMetaData()
	{
		global $katana;
		global $adc;
		$katana->setDetailView(false);
		if(self::getDescription() != '')
		{ $katana->setMetaData(self::getDescription()); }
		$katana->addBeforeMetaKey(self::getTitle());
	}
	
	public function getFormId() { return intval($this->FormId); }
	public function getFormBuyId() { return intval($this->FormBuyId); }
	public function getProductId() { return intval($this->ProductId); }
	public function getProductBuyId() { return intval($this->ProductBuyId); }
	public function getImageURL($w=0, $h=0) 
	{ 
		if(($w > 0) || ($h > 0))
		{ return ADTYPE_URL.self::getImage(); }
		else
		{ 
			if(is_object(self::getImageObject()))
			{ return self::getImageObject()->getLink($w, $h); }
		}
	}
	public function getImageThumbURL() { return ADTYPE_URL.'thumbs/'.self::getImage(); }
	public function getDescription()
	{
		global $adc;
		if($adc->getFrontend() > 0)
		{
			if(is_object($adc->CurrentAd))
			{
				if($adc->CurrentAd->getBrandId() > 0)
				{ $adc->setCurrentAdBrandId($adc->CurrentAd->getBrandId()); }
				if($adc->CurrentAd->getCatId() > 0)
				{ $adc->setCurrentAdCatId($adc->CurrentAd->getCatId()); }
			}
			if(($adc->getCurrentAdBrandId() > 0) || ($adc->getCurrentAdCatId() > 0))
			{
				$cat = $adc->getCurrentAdCat();
				$brand = $adc->getCurrentAdBrand();
				if((substr_count($this->Description, '<!--BrandTitle-->') > 0) && ($adc->getCurrentAdBrandId() > 0))
				{ $this->Description = str_replace('<!--BrandTitle-->', $adc->CurrentAdBrand->getTitle().' ',$this->Description); }
				if((substr_count($this->Description, '<!--CatTitle-->') > 0) && ($adc->getCurrentAdCatId() > 0))
				{ $this->Description = str_replace('<!--CatTitle-->', $adc->CurrentAdCat->getTitle(),$this->Description); }
				if((substr_count($this->Description, '<!--CatTitleLc-->') > 0) && ($adc->getCurrentAdCatId() > 0))
				{ $this->Description = str_replace('<!--CatTitleLc-->', strtolower($adc->CurrentAdCat->getTitle()),$this->Description); }
			}
		}
		return $this->Description;
	}
	
	public function setAdsCount($val) { $this->AdsCount=intval($val); }
	public function setFormId($val) { $this->FormId=intval($val); }
	public function setFormBuyId($val) { $this->FormBuyId=intval($val); }
	public function setProductId($val) { $this->ProductId=intval($val); }
	public function setProductBuyId($val) { $this->ProductBuyId=intval($val); }
	
	function update()
	{
		if((self::getId() > 0) && (self::getDataChanged()))
		{
			$sql  = 'UPDATE '.DB_PERFIX.self::getDataTable().' SET ';
			$sql .= '`FormId`='.self::getFormId().', ';
			$sql .= '`FormBuyId`='.self::getFormBuyId().', ';
			$sql .= '`AdTypeObject`="'.self::getAdTypeObject().'", ';
			$sql .= '`title`="'.self::formatText(self::getTitle(1)).'", ';
			$sql .= '`titleEn`="'.self::formatText(self::getTitleEn()).'", ';
			$sql .= '`description`="'.self::formatHTML(self::getDescription()).'", ';
			$sql .= '`image`="'.self::formatText(self::getImage()).'", ';
			$sql .= '`active`='.self::getActive().' ';
			$sql .= ' WHERE id='.self::getId().' LIMIT 1;';
			mysql_query($sql);
		}
	}
	
	function uploadImage($file)
	{
		global $thumb;
		self::setImage(self::uploadObjectImage(ADTYPE_PATH, $file, $thumb['ADTYPE']));
	}
	
	function deleteImage()
	{
		global $thumb;
		self::setImage(self::deleteObjectImage(ADTYPE_PATH, self::getImage(), $thumb['ADTYPE']));
	}
	
	function loadAdsCount()
	{
		if((self::getId() > 0) && (!self::getItemLoaded('AdsCount')))
		{
			global $db;
			$sql = 'SELECT COUNT(id) FROM '.DB_PERFIX.'am_ads WHERE typeId = '.self::getId().';';
			$result = $db->query($sql);
			$data = $db->fetch($sql, CACHE_TTL);
			if(is_array($data))
			{
				foreach($data as $row)
				{ self::setAdsCount($row['COUNT(id)']); }
			}
			
			unset($data);
			self::setItemLoaded('AdsCount', true);
		}
	}
}
?>